/*
 * Copyright (c) 2003
 * XDoclet Team
 * All rights reserved.
 */
package org.xdoclet.plugin.qtags.doclipse;

import org.generama.JellyTemplateEngine;
import org.generama.QDoxCapableMetadataProvider;
import org.generama.WriterMapper;

import org.xdoclet.plugin.qtags.AbstractQTagsPlugin;

import com.thoughtworks.qdox.model.DocletTag;
import com.thoughtworks.qdox.model.JavaClass;
import com.thoughtworks.qdox.model.JavaMethod;
import com.thoughtworks.qdox.model.Type;

/**
 * create doclipse tag definition file to support code completion in eclipse
 * @author Konstantin Pribluda
 * @version $Revision: 379 $
 */
public class QTagDoclipsePlugin extends AbstractQTagsPlugin {
    public QTagDoclipsePlugin(JellyTemplateEngine jellyTemplateEngine, QDoxCapableMetadataProvider metadataProvider,
        WriterMapper writerMapper) {
        super(jellyTemplateEngine, metadataProvider, writerMapper);
        setFilereplace("defintion.xml");
        setMultioutput(false);
    }

    /**
     *  compile list of allowed values
     * @param method to be searcher for allowed values
     * @return comma separated list of allowed values or null
     */
    public String getAllowedValues(JavaMethod method) {
        if (method.getReturns().getValue().equals("boolean") 
            || method.getReturns().getValue().equals("java.lang.Boolean")) {
//            || method.getReturns().isA(new Type("java.lang.Boolean"))) {
            return "true false";
        }

        if (method.getTagByName("qtags.allowed-value") == null) {
            return null;
        }

        StringBuffer allowed = new StringBuffer();
        DocletTag[] tags = method.getTagsByName("qtags.allowed-value");

        for (int i = 0; i < tags.length; i++) {
            allowed.append(tags[i].getValue());

            if (i < tags.length - 1) {
                allowed.append(' ');
            }
        }

        return allowed.toString();
    }

    /**
     * provide target specification ( comma separated )
     * @param metadata to process
     * @return
     */
    public String getTarget(JavaClass clazz) {
        DocletTag[] tags = clazz.getTagsByName("qtags.location");
        StringBuffer target = new StringBuffer();

        for (int i = 0; i < tags.length; i++) {
            target.append(tags[i].getValue());

            if (i < tags.length - 1) {
                target.append(' ');
            }
        }

        return target.toString();
    }

    public boolean shouldGenerate(Object metadata) {
        return qTagUtils.shouldGenerate(metadata);
    }
}